<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Classes extends Base
{
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/mlogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['sid'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
    }
    public function add(){
        $teacher = Db::name('teacher')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $this->assign('teacher_list',$teacher);
        return view('classes/add');
    }
    public function edit(){
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $teacher = Db::name('teacher')
              ->field('id,name')
              ->where(['status'=>1,'sid'=>$this->AdminAccount['sid']])
              ->order('id desc')->select();
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('classes')
                ->where($where)
                ->find();

        $this->assign('cid',$id);
        $this->assign('info',$info);
        $this->assign('teacher_list',$teacher);
        return view('classes/edit');
    }
    public function lists(){
        return view('classes/lists');
    }
   public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $sid = isset($input['sid'])?$input['sid']:0;
        if (!$sid) {
          $this->res_data['Msg'] = "缺少必填信息";
          return $this->res_data;
        }
        $store= Db::name('store')->where([['id','=',$sid]])->find();
        if (!$store) {
          $this->res_data['Msg'] = "无此门店";
          return $this->res_data;
        }
        $where = [];
        $where[] = ['sid','=',$sid];
        $where[] = ['status','=',1];
        $field = '*';
        $count=Db::name('classes')->where($where)->count();
        $list  = Db::name('classes')
              ->field($field)
              ->where($where)
              ->order('id desc')
              ->limit($page_start, $page_limit)->select();
        foreach($list as $k=>&$v){
            $v['master_teacher_name'] = Db::name('teacher')->where(['id'=>$v['master_teacher']])->value('name');
            $v['assistant_teacher_name'] = Db::name('teacher')->where(['id'=>$v['assistant_teacher']])->value('name');
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['classes'=>$list,'Data'=>$list,'recordsTotal'=>$count];
        return $this->print_result($this->res_data);
   }
    public function infoData(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        $where = [];
        $where[] = ['id','=',$id];
        $info = Db::name('classes')
                ->where($where)
                ->find();
        if ($info) {
            $info['master_teacher_name'] = Db::name('teacher')->where(['id'=>$info['master_teacher']])->value('name');
            $info['assistant_teacher_name'] = Db::name('teacher')->where(['id'=>$info['assistant_teacher']])->value('name');
        }
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['classes'=>$info];
        return $this->print_result($this->res_data);
   }
   public function doCreateClasses(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $classes = Controller('Classes', 'logic');
        $result            = $classes->create($param);
        return $this->print_result($result);
   }
   public function editClassesinfo(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $classes= Controller('Classes', 'logic');
        $result            = $classes->edit($param);
        return $this->print_result($result);
   }
   public function delete(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $classes = Controller('Classes', 'logic');
        $result            = $classes->delete($param);
        return $this->print_result($result);
   }
   // 班级花名册
   public function students_list()
   {
        $input = $this->request->param(false);
        $id = isset($input['cid'])?$input['cid']:0;
        if(!$id){
            $this->res_data['Msg'] = '参数有误';
            return $this->print_result($this->res_data);
        }
        // 查询课程再查询学生
        $students = [];
        $course = Db::name('course')->where(['classes_id'=>$id,'status'=>1])->find();
        if ($course) {
            $students = Db::name('user_course')->alias('uc')->leftJoin('user u','uc.user_id=u.id')->where(['uc.course_id'=>$course['id']])->field('u.name,u.photo')->select();
            if ($students) {
                foreach ($students as $key => &$value) {
                    $value['photo'] = config('upload_domain').$value['photo'];
                }
            } else {
                $students = [];
            }
        }

        $this->assign('cid',$id);
        $this->assign('students',$students);
        return view('classes/students_list');
   }
}
